package com.etboot.modules.base.controller.manage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etboot.common.utils.PageUtil;
import com.etboot.common.utils.ResultUtil;
import com.etboot.common.vo.PageVo;
import com.etboot.common.vo.Result;
import com.etboot.modules.activiti.config.ActivitiConstant;
import com.etboot.modules.base.entity.Message;
import com.etboot.modules.base.entity.MessageSend;
import com.etboot.modules.base.entity.User;
import com.etboot.modules.base.mpbase.controller.BaseController;
import com.etboot.modules.base.service.MessageSendService;
import com.etboot.modules.base.service.MessageService;
import com.etboot.modules.base.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;


/**
 * @author xboot
 */
@Slf4j
@RestController
@Api(description = "消息发送管理接口")
@RequestMapping("/xboot/messageSend")
@Transactional
public class MessageSendController { //extends BaseController<MessageSend, String> {
    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageSendService messageSendService;

    // @Override
    // public MessageSendService getService() {
    //     return messageSendService;
    // }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<MessageSend>> getByCondition(MessageSend ms, PageVo pv) {
        Page<MessageSend> page = messageSendService.findByCondition(ms, PageUtil.initMpPage(pv));
        // lambda
        page.getRecords().forEach(item -> {
            User u = userService.get(item.getUserId()); // getById
            if (u != null) {
                item.setUsername(u.getUsername()).setNickname(u.getNickname());
            }
            Message m = messageService.getById(item.getMessageId());
            item.setTitle(m.getTitle()).setContent(m.getContent()).setType(m.getType());
        });
        return new ResultUtil<Page<MessageSend>>().setData(page);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新消息")
    public Result<Page<MessageSend>> update(MessageSend ms) {
        messageSendService.updateById(ms);
        return ResultUtil.success("更新成功");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除消息")
    public Result<Object> delMessage(@RequestParam String[] ids) {
        messageSendService.removeByIds(Arrays.asList(ids));
        return ResultUtil.success("删除成功");
    }
}
